package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.GiftCertificate;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.GiftCertificateDao;
import com.weipai.springboot.module.mapper.CustomerGiftMapper;
import com.weipai.springboot.module.mapper.GiftCertificateMapper;
import com.weipai.springboot.module.service.GiftCertificateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/04 15:28
 * 
 * 礼券
 */
@Service
public class GiftCertificateServiceImpl implements GiftCertificateService{

    private static final Logger log = LoggerFactory.getLogger(GiftCertificateServiceImpl.class);

    static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    @Autowired
    private GiftCertificateDao giftCertificateDao;
    @Autowired
    private GiftCertificateMapper giftCertificateMapper;
    @Autowired
    private CustomerGiftMapper customerGiftMapper;

    //保存
    @Override
    public Response save(GiftCertificate giftCertificate) {
        Response response = new Response();
        if(1==giftCertificateDao.insert(giftCertificate)){
            response.setCode(RetCode.SUCCESS);
        }else {
            log.debug("保存礼券失败:{}",giftCertificate.getGiftCertificateName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //查询礼券详情
    @Override
    public Response getGiftCertificateById(String id) {
        Response response = new Response();
        GiftCertificate giftCertificate = giftCertificateDao.getGiftCertificateById(id);
        if(null!=giftCertificate){
            response.setCode(RetCode.SUCCESS);
            response.put("data", giftCertificate);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //删除
    @Override
    public Response deleteById(String id) {
        Response response = new Response();
        GiftCertificate giftCertificate = giftCertificateMapper.selectByPrimaryKey(id);

        try {
            // 优惠券过期时间 > 系统时间
            long time = format.parse(giftCertificate.getGiftCertificateEnd()).getTime();
            if(time > System.currentTimeMillis()){
                response.setCode(RetCode.NO_GiftCertificate);
            }else{
                if(1==giftCertificateDao.updateDel(id)){
                    response.setCode(RetCode.SUCCESS);
                }else {
                    response.setCode(RetCode.NO_DATA);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return response;
    }

    //更改礼券的状态--启用/禁用
    @Override
    public Response changeStatus(String id, Integer status) {
        Response response = new Response();
        if(1==giftCertificateDao.updateStatus(id, status)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //修改礼券
    @Override
    public Response update(GiftCertificate giftCertificate) {
        Response response = new Response();
        if(1==giftCertificateDao.updateByPrimaryKey(giftCertificate)){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.debug("修改礼券失败:{}",giftCertificate.getGiftCertificateName());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //分页查询
    @Override
    public Response selectAllByPage(Integer page, Integer size, String draw){
        Integer start = (page-1) * size;
        List<Map<String, Object>> giftCertificateList = giftCertificateDao.selectAllByPage(start, size);
        // 领券中心  标记 已领过的优惠券
          Integer num = giftCertificateList.size();
          for (int i = 0; i < num; i++) {
              String  giftCertificateId = giftCertificateList.get(i).get("giftCertificateId").toString();
                if(giftCertificateId!="" && giftCertificateId!=null && draw!="" && draw!=null){
                    //验证用户是否领取过
                   Integer number = customerGiftMapper.verifiCustomerGift(draw,giftCertificateId);
                    if(number>0){
                        giftCertificateList.get(i).put("isReceive",1);
                    }else{
                        giftCertificateList.get(i).put("isReceive",0);
                    }
                }
           }
        Integer totalNum = giftCertificateDao.selectAllByPageAmount();
        PageResult<Map<String, Object>> pageList = new PageResult<>(giftCertificateList,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }


    @Override
    public Response selectAllByPage22(Integer page, Integer size, String draw) {
        Integer start = (page-1) * size;
        List<Map<String, Object>> giftCertificateList = giftCertificateMapper.selectAllByPage22(start, size);
        Integer totalNum = giftCertificateMapper.selectAllByPageAmount22();
        PageResult<Map<String, Object>> pageList = new PageResult<>(giftCertificateList,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }



    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();

        if(id.length>0) {
            for (String ids : id) {
                GiftCertificate giftCertificate = giftCertificateMapper.selectByPrimaryKey(ids);
                try {
                    // 优惠券过期时间 > 系统时间
                    long time = format.parse(giftCertificate.getGiftCertificateEnd()).getTime();
                    if(time > System.currentTimeMillis()){
                        response.setCode(RetCode.NO_GiftCertificate);
                    }else{
                        if(giftCertificateDao.updateDel(ids)>0){
                            response.setCode(RetCode.SUCCESS);
                        }else {
                            response.setCode(RetCode.REQUEST_ERROR);
                        }

                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }


        
        return response;
    }

    //定时任务
    @Override
    public void disposeGiftCertificate() {
        List<GiftCertificate> giftCertificates = giftCertificateDao.getAll();
        if(!giftCertificates.isEmpty()){
            for(GiftCertificate giftCertificate : giftCertificates){
                try {
                    // 优惠券过期时间 <= 系统时间
                    long time = format.parse(giftCertificate.getGiftCertificateEnd()).getTime();
                    if(time <= System.currentTimeMillis()){
                        giftCertificate.setGiftCertificateStatus(-1);
                        giftCertificateMapper.updateByPrimaryKeySelective(giftCertificate);
                    }
                } catch (ParseException e) {
                    log.error("giftCate timer error: {}",e);
                }
            }
        }
    }


}
